//
//  GAKKIBaseViewController.m
//  GANGAIKit
//
//  Created by fh on 2019/11/5.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBaseViewController.h"
#import "GAKKIHUD.h"
#import "UIBarButtonItem+Factory.h"

@interface GAKKIBaseViewController ()

/// 用于屏幕尺寸不够的子View滚动容器
@property (nonatomic, readwrite, strong) UIScrollView *contentScrollView;
/// 用于屏幕尺寸不够的子View容器
@property (nonatomic, readwrite, strong) UIView *contentView;
/// 控制器需要从外界获取的参数,只能通过initWithParams:配置
@property (nonatomic, readonly, copy) NSDictionary *params;

// FastNavigationItem
/// 私有左BarButtonItem事件
@property (nonatomic, readwrite, copy) void(^inner_leftBarButtonItemBlock)(UIBarButtonItem *barButtonItem, GAKKIBaseViewController *innerWeakSelf);
/// 私有右BarButtonItem事件
@property (nonatomic, readwrite, copy) void(^inner_rightBarButtonItemBlock)(UIBarButtonItem *barButtonItem, GAKKIBaseViewController *innerWeakSelf);

@end

@implementation GAKKIBaseViewController

#pragma mark - Life

+ (instancetype)new {
    return [[NSClassFromString([NSString stringWithUTF8String:object_getClassName(self)]) alloc] init];
}

/// 初始化
- (instancetype)init {
    if (self = [super init]) {
        [self inner_baseViewControllerDefaultInitSetup];
    }
    return self;
}

/// 用传递参数初始化
/// @param params 传递参数
- (instancetype)initWithParams:(NSDictionary *)params {
    if (self = [self init]) {
        _params = params;
    }
    return self;
}

- (instancetype)initWithParams:(NSDictionary *)params settingBlock:(void (^)(GAKKIBaseViewController * _Nonnull))settingBlock {
    if (self = [self initWithParams:params]) {
        if (settingBlock) {
            settingBlock(self);
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (self.shouldAutoConfigureViewOnViewDidLoad) {
        [self setupView];
    }
    if (self.shouldRequestRemoteDataOnViewDidLoad) {
        [self requestRemoteData];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:self.prefersNavigationBarHidden animated:animated];
}

- (void)dealloc {
    if (kDebugViewControllerRelease) {
        DBLog(@"\n ~~~~~~+++++++ 控制器 %@ 销毁了 +++++++~~~~~~ \n",[self class])
    }
}

#pragma mark - Public
/// 布局:子类重写进行布局
- (void)setupView {
    // 子类实现
}

/// 请求网络数据:子类重写，不需要调用父类
- (void)requestRemoteData {
    // 子类实现
}

/// 存储数据到本地/数据库:子类实现,基类只是提供接口
- (void)storeIntoDBData {
    // 子类实现
}

/// 获取本地数据/数据库:子类实现,基类只是提供接口
- (void)fetchDBData {
    // 子类实现
}

/// 可以滚动布局:在block里添加子控件，添加到contentView上并设置约束,最后需要设置contentView的底部或右边的约束,参考masonry的scrollView布局约束方法
/// @param isHorizontal 是否是水平滚动
/// @param contentViewBlock 内容父类视图
- (void)configScrollableViewWithDirection:(BOOL)isHorizontal realSuperContentViewCallbackBlock:(void(^)(UIView *contentView))contentViewBlock {
    WeakSelf;
    [self.view addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.contentView];
    [self.contentScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.contentScrollView);
        if (isHorizontal) {
            make.height.equalTo(weakSelf.contentScrollView.mas_height);
        }else {
            make.width.equalTo(weakSelf.contentScrollView.mas_width);
        }
    }];
    if (contentViewBlock) {
        contentViewBlock(weakSelf.contentView);
    }
}

#pragma mark - Private

/// 初始化默认参数
- (void)inner_baseViewControllerDefaultInitSetup {
    // 默认在viewDidLoad里面加载布局
    _shouldAutoConfigureViewOnViewDidLoad = YES;
    // 默认在viewDidLoad里面请求服务器的数据
    _shouldRequestRemoteDataOnViewDidLoad = YES;
    // 默认不隐藏导航栏
    _prefersNavigationBarHidden = NO;
}

#pragma mark - Setter and getter
- (UIScrollView *)contentScrollView {
    if (!_contentScrollView) {
        _contentScrollView = [UIScrollView new];
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _contentScrollView;
}
- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [UIView new];
    }
    return _contentView;
}

@end


/// 指示功能
@implementation GAKKIBaseViewController (Indicate)

/// 简单文本弹框提醒
/// @param remindText 弹框文本
- (void)showTipViewWithInfo:(NSString *)remindText {
    [[GAKKIHUD shareGAKKIHUD] showTipViewWithInfo:remindText toView:nil];
}

/// 简单文本弹框提醒
/// @param remindText 弹框文本
/// @param view 父视图
- (void)showTipViewWithInfo:(NSString *)remindText toView:(UIView *)view {
    [[GAKKIHUD shareGAKKIHUD] showTipViewWithInfo:remindText toView:view];
}

/// 简单加载中提醒
/// @param remindText 加载文本
- (void)showLoadingWithRemindText:(NSString *)remindText {
    [[GAKKIHUD shareGAKKIHUD] showLoadingViewWithInfo:remindText toView:nil];
}

/// 简单加载中提醒
/// @param remindText 加载文本
/// @param view 父视图
- (void)showLoadingWithRemindText:(NSString *)remindText toView:(UIView *)view {
    [[GAKKIHUD shareGAKKIHUD] showLoadingViewWithInfo:remindText toView:view];
}

/// 隐藏加载提醒
- (void)hiddenLoadingRemindText {
    [[GAKKIHUD shareGAKKIHUD] hiddenLoadingView];
}

@end


/// 导航栏控件功能
@implementation GAKKIBaseViewController (FastNavigationItem)

#pragma mark - Public

/// 快速添加左边按钮: 后续需要根据项目修改需求,需要修改同一字体样式和大小
/// @param title 按钮标题
/// @param actionBlock 按钮方法:回调出生成的UIBarButtonItem和自身的弱引用便于外部调用
- (void)addFastLeftBarButtonItemWithTitle:(NSString *)title action:(void(^)(UIBarButtonItem *item, GAKKIBaseViewController *blockInnerWeakSelf))actionBlock {
    if (!self.navigationController) {
        return;
    }
    if (actionBlock) {
        self.inner_leftBarButtonItemBlock = actionBlock;
    }
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem gakki_systemItemWithTitle:title titleColor:[UIColor blackColor] imageName:nil target:self selector:@selector(leftBarButtonItemAction:) textType:YES];
}

/// 快速添加左边按钮: 后续需要根据项目修改需求,需要修改同一字体样式和大小
/// @param imgStr 图片名称
/// @param actionBlock 按钮方法:回调出生成的UIBarButtonItem和自身的弱引用便于外部调用
- (void)addFastLeftBarButtonItemWithImageStr:(NSString *)imgStr action:(void(^)(UIBarButtonItem *item, GAKKIBaseViewController *blockInnerWeakSelf))actionBlock {
    if (!self.navigationController) {
        return;
    }
    if (actionBlock) {
        self.inner_leftBarButtonItemBlock = actionBlock;
    }
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem gakki_systemItemWithTitle:nil titleColor:[UIColor blackColor] imageName:imgStr target:self selector:@selector(leftBarButtonItemAction:) textType:NO];
}

/// 快速添加右边按钮: 后续需要根据项目修改需求,需要修改同一字体样式和大小
/// @param title 按钮标题
/// @param actionBlock 按钮方法:回调出生成的UIBarButtonItem和自身的弱引用便于外部调用
- (void)addFastRightBarButtonItemWithTitle:(NSString *)title action:(void(^)(UIBarButtonItem *item, GAKKIBaseViewController *blockInnerWeakSelf))actionBlock {
    if (!self.navigationController) {
        return;
    }
    if (actionBlock) {
        self.inner_rightBarButtonItemBlock = actionBlock;
    }
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem gakki_systemItemWithTitle:title titleColor:[UIColor redColor] imageName:nil target:self selector:@selector(rightBarButtonItemAction:) textType:YES];
}

/// 快速添加右边按钮: 后续需要根据项目修改需求,需要修改同一字体样式和大小
/// @param imgStr 图片名称
/// @param actionBlock 按钮方法:回调出生成的UIBarButtonItem和自身的弱引用便于外部调用
- (void)addFastRightBarButtonItemWithImageStr:(NSString *)imgStr action:(void(^)(UIBarButtonItem *item, GAKKIBaseViewController *blockInnerWeakSelf))actionBlock {
    if (!self.navigationController) {
        return;
    }
    if (actionBlock) {
        self.inner_rightBarButtonItemBlock = actionBlock;
    }
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem gakki_systemItemWithTitle:nil titleColor:[UIColor blueColor] imageName:imgStr target:self selector:@selector(rightBarButtonItemAction:) textType:NO];
}

/// 自定义返回方法
/// @param actionBlock 按钮方法
- (void)customLeftBarButtonItemActionWithAction:(void(^)(UIBarButtonItem *item, GAKKIBaseViewController *blockInnerWeakSelf))actionBlock {
    if (!self.navigationController) {
        return;
    }
    if (actionBlock) {
        self.inner_rightBarButtonItemBlock = actionBlock;
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"navigationBar_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
}

#pragma mark - Event

/// 左事件
/// @param barButtonItem 左BarButtonItem
- (void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem {
    WeakSelf;
    if (self.inner_leftBarButtonItemBlock) {
        self.inner_leftBarButtonItemBlock(barButtonItem,weakSelf);
    }
}

/// 右事件
/// @param barButtonItem 右BarButtonItem
- (void)rightBarButtonItemAction:(UIBarButtonItem *)barButtonItem {
    WeakSelf;
    if (self.inner_rightBarButtonItemBlock) {
        self.inner_rightBarButtonItemBlock(barButtonItem,weakSelf);
    }
}

@end
